// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import Order from "../order/Order";
import AudioManage from "../manage/AudioManage";
import GameTools from "../gameTool/GameTools";
import func from "../config/func";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Material extends cc.Component {

    @property(cc.Sprite)
    m_bg: cc.Sprite = null;

    n_moveSpeed: number = 75;
    n_ani: cc.Tween = null;
    n_orderNode: Order = null;
    n_type: number = 0;
    start() {
        this.n_orderNode = cc.find("Canvas/game/orderNode").getComponent(Order);
    }

    init(type: number) {
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchItem, this);
        this.n_type = type;
        let distanceTransition = config.startPosition.sub(config.transitionPosition).mag(),
            moveTransitionTime = distanceTransition / this.n_moveSpeed,
            distanceEndion = config.transitionPosition.sub(config.endPosition).mag(),
            moveeEndTime = distanceEndion / this.n_moveSpeed;
        this.n_ani = cc.tween(this.node)
            .to(0.1, { scale: 1 })
            .to(moveTransitionTime, { x: config.transitionPosition.x, y: config.transitionPosition.y })
            .to(moveeEndTime, { x: config.endPosition.x, y: config.endPosition.y })
            .to(0.1, { scale: 0 })
            .call(() => {
                this.node.off(cc.Node.EventType.TOUCH_END, this.touchItem, this);
                GameTools.gameTools.put(this.node, config.nodeToolPath.material);
            })
        this.n_ani.start()
        this.loadPlist();
    }
    /**
     * 加载图集
     */
    async loadPlist() {
        let plist: cc.SpriteAtlas = null;
        if (config.spriteAtlas[config.spriteAtlasPath.food]) {
            plist = config.spriteAtlas[config.spriteAtlasPath.food];
        } else {
            plist = await func.loadResouce(config.spriteAtlasPath.food, cc.SpriteAtlas) as cc.SpriteAtlas;
            config.spriteAtlas[config.spriteAtlasPath.food] = plist;
        }
        let key = `batching_${this.n_type}`;
        this.m_bg.spriteFrame = plist.getSpriteFrame(key);
    }
    /**
     * 回收材料
     */
    recoveryMaterial() {
        this.n_ani.stop();
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchItem, this);
        GameTools.gameTools.put(this.node, config.nodeToolPath.material);
    }

    touchItem() {
        if (this.n_ani == null || config.gameOver) return;
        if (!this.n_orderNode.checkReadyNode()) {
            return
        }
        if (config.orderArr.length >= 4) {
            console.log("没有可烹饪的订单");
            return
        }
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchItem, this);
        this.n_ani.stop();
        this.n_orderNode.selectMaterial(this.node, this.n_type);
    }

}
